package com.ghost.alibabaghost.service.sys;

import com.ghost.alibabaghost.api.service.sys.GhostUsersService;
import com.ghost.alibabaghost.api.model.sys.GhostUsersModel;
import com.ghost.alibabaghost.dao.dataobject.sys.GhostUsersDo;
import com.ghost.alibabaghost.dao.mapper.sys.GhostUsersMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息表(LhUsersModel)表服务实现类
 *
 * @author yaolisheng
 * @since 2022-01-08 02:33:45
 */
@Service("ghostUsersServiceImpl")
public class GhostUsersServiceImpl implements GhostUsersService {
    @Resource
    private GhostUsersMapper ghostUsersMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GhostUsersModel queryById(Long id) {
        GhostUsersDo lhUsersDo = this.ghostUsersMapper.queryById(id);
        GhostUsersModel model = new GhostUsersModel();
        BeanUtils.copyProperties(lhUsersDo, model);
        return model;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<GhostUsersModel> queryAllByLimit(int offset, int limit, GhostUsersModel lhUsersModel) {
        GhostUsersDo lhUsersDo = new GhostUsersDo();
        BeanUtils.copyProperties(lhUsersModel,lhUsersDo);
        List<GhostUsersDo> lhUsersDos = this.ghostUsersMapper.queryAllByLimit(offset, limit,lhUsersDo);
        List<GhostUsersModel> usersModelList = lhUsersDos.stream().map(usersDao -> {
            GhostUsersModel model = new GhostUsersModel();
            BeanUtils.copyProperties(usersDao, model);
            return model;
        }).collect(Collectors.toList());
        return usersModelList;
    }

    @Override
    public List<GhostUsersModel> queryAll(GhostUsersModel lhUsers) {
        GhostUsersDo usersDo = new GhostUsersDo();
        BeanUtils.copyProperties(lhUsers, usersDo);
        List<GhostUsersDo> lhUsersDos = ghostUsersMapper.queryAll(usersDo);
        List<GhostUsersModel> collect = lhUsersDos.stream().map(lhUsersDo -> {
            GhostUsersModel model = new GhostUsersModel();
            BeanUtils.copyProperties(lhUsersDo, model);
            return model;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public GhostUsersModel queryForLogin(String username, String password) {
        GhostUsersDo usersDo = ghostUsersMapper.queryForLogin(username, password);
        GhostUsersModel lhUsersModel = new GhostUsersModel();
        BeanUtils.copyProperties(usersDo, lhUsersModel);
        return lhUsersModel;
    }

    /**
     * 新增数据
     *
     * @param lhUsersModel 实例对象
     * @return 实例对象
     */
    @Override
    public GhostUsersModel insert(GhostUsersModel lhUsersModel) {
        GhostUsersDo usersDo = new GhostUsersDo();
        BeanUtils.copyProperties(lhUsersModel, usersDo);
        int insert = this.ghostUsersMapper.insert(usersDo);
        lhUsersModel.setId(insert);
        return lhUsersModel;
    }

    /**
     * 修改数据
     *
     * @param lhUsersModel 实例对象
     * @return 实例对象
     */
    @Override
    public Integer update(GhostUsersModel lhUsersModel) {
        GhostUsersDo usersDo = new GhostUsersDo();
        BeanUtils.copyProperties(lhUsersModel, usersDo);
        int update = this.ghostUsersMapper.update(usersDo);
        return update;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.ghostUsersMapper.deleteById(id) > 0;
    }

    @Override
    public Integer changeStatus(Integer id, Boolean status) {
        Integer num = ghostUsersMapper.changeStatus(id,status);
        return num;
    }


}
